package com.vilen.weixin.service;

import com.vilen.basic.model.WeixinMenu;
import com.vilen.basic.service.IWeixinMenuService;
import com.vilen.basic.util.JsonUtil;
import com.vilen.weixin.kit.WeixinBasicKit;
import com.vilen.weixin.model.WeixinFinalValue;
import com.vilen.weixin.model.WeixinMenuDto;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/2/16.
 */
@Service("wMenuService")
public class WMenuService implements IWMenuService{
    @Inject
    private IWeixinMenuService weixinMenuService;

    @Override
    public void publishMenu() {
        String url = WeixinBasicKit.replaceAccessTokenUrl(WeixinFinalValue.MENU_ADD);
        //TODO
        System.out.println(url);
        List<WeixinMenuDto> wmds = weixinMenuService.generateWeixinMenuDto();
        //前面有button
        Map<String,List<WeixinMenuDto>> maps = new HashMap<String, List<WeixinMenuDto>>();
        maps.put("button",wmds);
        String rel = WeixinBasicKit.sendJsonPost(url, JsonUtil.getInstance().obj2json(maps));
        if(!WeixinBasicKit.checkRequestSucc(rel)) {
            throw new RuntimeException("发布菜单失败:"+WeixinBasicKit.getRequestMsg(rel)+"错误代码："+WeixinBasicKit.getRequestCode(rel));
        }
    }

    @Override
    public String queryMenu() {
        String url = WeixinBasicKit.replaceAccessTokenUrl(WeixinFinalValue.MENU_QUERY);
        return WeixinBasicKit.sendGet(url);
    }
}
